<script type="text/javascript">
  RED.nodes.registerType('ads-client-read-runtime-state', {
    paletteLabel: 'ADS - Read Runtime State',
    category: 'TwinCAT ADS',
    icon: 'font-awesome/fa-refresh',
    color: '#3FADB5',
    inputs: 1,
    inputLabels: function() {
      return this.adsPort === '' ? "Topic as ADS port" : "Read trigger"
    },
    outputs: 1,
    outputLabels: 'Data',
    defaults: { 
      name: { value: '' },
      connection: { value: null, type: "ads-client-connection" },
      adsPort: { value: 851, required: false, validate: (v) => (v === '' || !isNaN(parseInt(v))) }
    },
    label: function () {
      if(this.connection === null)
        return  `${(this.name || `ADS - Read Runtime State`)} (*Not configured*)`;
 
      if (this.name) {
        return this.name
      }

      return `ADS - Read Runtime State (${(this.adsPort === '' ? `msg.topic` : `${this.adsPort}`)})`;
    },
    oneditprepare: function () {
      //Stuff that is done when properties panel is opened
    }
  })
</script>



<!-- Properties -->
<script type="text/html" data-template-name="ads-client-read-runtime-state">
  <div class="form-row">
      <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
      <input type="text" id="node-input-name" placeholder="Name (optional)">
  </div>

  <div class="form-row">
      <label for="node-input-connection"><i class="fa fa-bookmark"></i> ADS connection</label>
      <input type="text" id="node-input-connection" placeholder="ADS Connection">
  </div>

  <div class="form-row">
      <label for="node-input-adsPort"><i class="fa fa-tag"></i> ADS port (runtime)</label>
      <input type="text" id="node-input-adsPort" placeholder="ADS port (runtime)">
  </div>
  <div class="form-tips"><b>Tip:</b> Leave ADS port empty to use <code>msg.topic</code> input instead.</div>

  <div class="form-row">
    <!-- Some margin after tip -->
  </div>
</script>



<!-- Help -->
<script type="text/html" data-help-name="ads-client-read-runtime-state">
<p>
  Reads TwinCAT PLC runtime state from given ADS port.
</p>


<h3>Properties (settings)</h3>
  <dl class="message-properties">
    <dt>Name
      <span class="property-type">string</span>
    </dt>
    <dd> Optional node name</dd>
  </dl>
  
  <dl class="message-properties">
    <dt>ADS connection
      <span class="property-type">ads-client-connection</span>
    </dt>
    <dd> ADS client connection (target system) to use</dd>
  </dl>

  <dl class="message-properties">
    <dt>ADS port (runtime)
      <span class="property-type">number | string</span>
    </dt>
    <dd> ADS port of the target runtime (TC2: 801, 801.., TC3: 851, 852..)</dd>
  </dl>


<h3>Inputs</h3>
  <dl class="message-properties">
    <dt>topic
      <span class="property-type">undefined | number | string</span>
    </dt>
    <dd> If given and no ADS port set in properties, this topic is used as ADS port.</code></dd>
  </dl>

<h3>Outputs</h3>
  <dl class="message-properties">
    <dt>payload <span class="property-type">string</span></dt>
    <dd>The PLC runtime state as string (Run, Stop.. - See <a href="https://github.com/jisotalo/ads-client/blob/f1a5a07bd0c847b8a92a512c8b99b78eb7838b88/src/ads-client-ads.js#L469" target="_blank">ADS_STATE</a>)</dd>
    <dt>result <span class="property-type">object</span></dt>
    <dd>The PLC runtime state object</dd>
    <dt>... <span class="property-type">any</span></dt>
    <dd>All input <code>msg</code> properties are forwarded</dd>
  </dl>

<h3>Details</h3>
  <p>
  If the ADS port is not set in properties, the input <code>msg.topic</code> is used as ADS port instead.
  </p>
  <p>
  See <code>ads-client</code> readme for help.
  </p>


<h3>References</h3>
  <ul>
    <li><a href="https://github.com/jisotalo/ads-client#starting-and-stopping-the-plc" target="_blank" style="text-decoration: underline">
      ads-client readme
    </a></li>
    <li><a href="https://jisotalo.github.io/ads-client/Client.html#readPlcRuntimeState" target="_blank" style="text-decoration: underline">
      ads-client readPlcRuntimeState() documentation
    </a></li>
    <li><a href="https://github.com/jisotalo/ads-client/blob/f1a5a07bd0c847b8a92a512c8b99b78eb7838b88/src/ads-client-ads.js#L469" target="_blank" style="text-decoration: underline">
      Possible states (ADS_STATE)
    </a></li>
  </ul>
</script>